<?php
// Library File
include 'lib/library.php';

login_check();

$mode = mysql_real_escape_string($_GET['mode']);

switch($mode) {
	case NULL:
		$title = 'Edit Account';
		break;
	case "profile":
		$title = 'Edit Profile';
		break;
	case "items":
		$title = "Edit Items";
		break;
}
$subtitle = $title;

if($mode == 'profile'){
	// Ajax functionality
	$scripts .= '
		<script language="javascript" type="text/javascript" src="'.$addr.'/scripts/hide_on_profile.js"></script>
		<script language="javascript" type="text/javascript" src="'.$addr.'/scripts/update_profile.js"></script>
	';
	$profile_id = $m_user_id;
	$query = mysql_query("SELECT * FROM users WHERE user_id='$profile_id'");
	$user_array = mysql_fetch_assoc($query);
	$query = mysql_query("SELECT * FROM profiles where user_id='$profile_id'");
	$profile_array = mysql_fetch_assoc($query);
	$keys = array_keys($profile_array);
	
	if(!empty($user_array['name'])){
			
		$size = count($profile_array);
		for($x=3; $x < $size;$x++){
			$key = $keys[$x];
			$name = ucwords(str_replace('_', ' ', $key));
			
			$checked = NULL;
			if($profile_array[$key] == 0){
				$checked = 'CHECKED';
			}
			
			$output = false;		
			if($key == 'gender' && $m_member_type == 'IND'){
				$opposite = array('Male' => 'Female','Female' => 'Male');
				$gender = $user_array[$key];
				// Input
				$content .='
					<P>
					<label for="'.$x.'" class="nice">'.$name.':</label>
					<select class="nice '.$checked.'" name="gender" id="'.$x.'">
					<option value="'.$gender.'">'.$gender.'</option>
					<option value="'.$opposite[$gender].'">'.$opposite[$gender].'</option>
					</select>
				';
				$output = true;
			}else if($key == 'birthday' && $m_member_type == 'IND'){
				$value = trim($user_array[$key]);
				$value_array = explode('/',$value);
				// Input
				$content .='
					<P>
					<label for="'.$x.'1" class="nice">'.$name.':</label> 
					<input class="nice '.$checked.'" type="text" size="3" id="'.$x.'1" name="'.$key.'1" value="'.$value_array[0].'" /> / 
					<input class="'.$checked.'" type="text" size="3" id="'.$x.'2" name="'.$key.'2" value="'.$value_array[1].'" /> / 
					<input class="'.$checked.'" type="text" size="6" id="'.$x.'3" name="'.$key.'3" value="'.$value_array[2].'" />
				';
				$output = true;
			}else if($key == 'name' && $m_member_type == 'IND'){
				$value = trim($user_array[$key]);
				$value_array = explode(' ',$value);
				// Input
				$content .='
					<P>
					<label for="'.$x.'1" class="nice">'.$name.':</label> 
					<input class="nice '.$checked.'" type="text" size="10" id="'.$x.'1" name="'.$key.'1" title="First Name" value="'.$value_array[0].'" />
					<input class="'.$checked.'" type="text" size="2" id="'.$x.'2" name="'.$key.'2" title="Middle Initial" value="'.$value_array[1].'" />
					<input class="'.$checked.'" type="text" size="10" id="'.$x.'3" name="'.$key.'3" title="Last Name" value="'.$value_array[2].'" />
					<input class="'.$checked.'" type="text" size="2" id="'.$x.'4" name="'.$key.'4" title="Suffix" value="'.$value_array[3].'" />
				';
				$output = true;
			}else if($key == 'phone_number'){
				$value = trim($user_array[$key]);
				$value_array = explode(' ',$value);
				// Input
				$content .='
					<P>
					<label for="'.$x.'1" class="nice">'.$name.':</label> 
					<input class="nice '.$checked.'" type="text" size="3" id="'.$x.'1"  name="'.$key.'1" title="Area Code" value="'.$value_array[0].'" />
					<input class="'.$checked.'" type="text" size="3" id="'.$x.'2" name="'.$key.'2" title="First 3 Digits" value="'.$value_array[1].'" /> -
					<input class="'.$checked.'" type="text" size="8" id="'.$x.'3" name="'.$key.'3" title="Last 4 Digits" value="'.$value_array[3].'" />
				';
				$output = true;
			}
			
			if(!$output){
				// Input
				$content .='
					<P>
					<label for="'.$x.'" class="nice">'.$name.':</label> 
					<input class="nice '.$checked.'" type="text" id="'.$x.'" name="'.$key.'" value="'.$user_array[$key].'" />
				';
			}
			
			$content .= '
				<label for="'.$x.'h" class="small">Hide?</label>
				<input type="checkbox" name="hide_'.$key.'" id="'.$x.'h" onclick="hide_on_profile(\''.$key.'\',\''.$x.'\')" '.$checked.'/>
				<input type="button" name="update_'.$key.'" value="update" onclick="update_profile(\''.$key.'\',\''.$x.'\')"/>
				</P>
				<P class="error under_input" id="error_'.$key.'"></P>
			';
		}
		
	}
}


$side = '
	<a href="'.$addr.'/editaccount.php">Edit Account</a>
	<a href="'.$addr.'/editaccount.php?mode=profile">Edit Profile</a>
	<a href="'.$addr.'/editaccount.php?mode=items">Edit Items</a>
';

/*
**   P U T    V A R S
**    O N    P A G E
*/
	head($title,$style,$scripts);
	body($header,$personal,$subtitle,$content,$navigation,$side);
	foot($footer,$dbc);
?>
